
Page({
  data: {
    msg:"",
    todo: [],
    done: []
  },

  onLoad() {
    let todo = wx.getStorageSync('todo')
    let done = wx.getStorageSync('done')
    if(todo) this.setData({todo})
    if(done) this.setData({done})
  },
  handleEnter(event){
    let todo = this.data.todo;
    let value = event.detail.value;
    if(todo.includes(value)) return;
    todo.push({value,checked:false});
    this.setData({todo,msg:""})
    this.setStorage()
  },

  handleDelete(event){
    let {index,type} = event.currentTarget.dataset;
    let {todo,done} = this.data;
    if(type === "todo"){
      todo.splice(index,1)
    }else if(type === "done"){
      done.splice(index,1)
    }
    this.setData({todo,done})
    this.setStorage()
  },

  handleChecked(event){
    let {index,type} = event.currentTarget.dataset;
    let {todo,done} = this.data;
    if(type === "todo"){
      let del = todo.splice(index,1)
      del[0].checked = true
      done.unshift(...del)
    }else if(type === "done"){
      let del = done.splice(index,1)
      del[0].checked = false
      todo.push(...del)
    }
    this.setData({todo,done})
    this.setStorage()
  },

  setStorage(){
    let {todo,done} = this.data;
    wx.setStorageSync('todo', todo);
    wx.setStorageSync('done', done)
  }
})
